<?php
namespace WeChat;
use Swiftx\Base\Object;
/**
 * ---------------------------------------------------------------------------------------------------------------------
 * 微信连接器
 * ---------------------------------------------------------------------------------------------------------------------
 * @author		胡永强  <odaytudio@gmail.com>
 * @since		2015-09-29
 * @copyright	Copyright (c) 2014-2015 Swiftx Inc.
 * ---------------------------------------------------------------------------------------------------------------------
 * @property-read string $AppId
 * @property-read string $Secret
 * @property-read Token $AccessToken
 * @property-read Ticket $JsApiTicket
 * ---------------------------------------------------------------------------------------------------------------------
 */
abstract class Connector extends Object {

    protected $_AppId = null;
    protected $_Secret = null;
    protected $_AccessToken = null;
    protected $_JsApiTicket = null;

    /**
     * 私有构造
     */
    abstract protected function __construct();

    /**
     * 获取AppID
     * @return string
     */
    protected function _getAppID(){
        return $this->_AppId;
    }

    /**
     * 获取Secret
     * @return string
     */
    protected function _getSecret(){
        return $this->_Secret;
    }

    /**
     * 获取AccessToken
     * @return Token
     * @throws Exception
     */
    protected function _getAccessToken(){
        if($this->_AccessToken instanceof Token and time() < $this->AccessToken->OverTime)
            return $this->_AccessToken;
        if(file_exists($this->_AccessToken)) {
            $AccessToken = file_get_contents($this->_AccessToken);
            $AccessToken = explode(',', $AccessToken);
            if(time() < $AccessToken[1]+$AccessToken[2])
                return new Token($AccessToken[0], $AccessToken[1], $AccessToken[2]);
        }
        $AccessToken = Api::FetchAccessToken($this->_AppId, $this->_Secret);
        $Content = $AccessToken->Key.','.$AccessToken->Expires.','.$AccessToken->CreateTime;
        file_put_contents($this->_AccessToken, $Content);
        return $this->_AccessToken = $AccessToken;
    }

    /**
     * 获取AccessToken
     * @return Ticket
     * @throws Exception
     */
    protected function _getJsApiTicket(){
        if($this->_JsApiTicket instanceof Ticket and time() < $this->JsApiTicket->OverTime)
            return $this->_JsApiTicket;
        if(file_exists($this->_JsApiTicket)) {
            $JsApiTicket = file_get_contents($this->_JsApiTicket);
            $JsApiTicket = explode(',', $JsApiTicket);
            if(time() < $JsApiTicket[1]+$JsApiTicket[2])
                return new Ticket($JsApiTicket[0], $JsApiTicket[1], $JsApiTicket[2]);
        }
        $JsApiTicket = Api::FetchJsApiTicket($this->AccessToken->Key);
        $Content = $JsApiTicket->Key.','.$JsApiTicket->Expires.','.$JsApiTicket->CreateTime;
        file_put_contents($this->_JsApiTicket, $Content);
        return $this->_JsApiTicket = $JsApiTicket;
    }

    /**
     * 生成JsSdk
     * @param $url
     * @return JsSdk
     */
    public function MakeJsSdk($url){
        return new JsSdk($this, $url);
    }


}
